# -*- coding:utf-8 -*-

from typing import List
from sqlalchemy.ext.asyncio import AsyncSession
from utils.common_util import CamelCaseUtil, export_list2excel
from utils.page_util import PageResponseModel
from module_admin.dao.seedlings_dao import SeedlingsDao
from module_admin.entity.do.seedlings_do import Seedlings
from module_admin.entity.vo.seedlings_vo import SeedlingsPageModel, SeedlingsModel


class SeedlingsService:
    """
    用户管理模块服务层
    """

    @classmethod
    async def get_seedlings_list(cls, query_db: AsyncSession, query_object: SeedlingsPageModel, data_scope_sql: str) -> [list | PageResponseModel]:
        seedlings_list = await SeedlingsDao.get_seedlings_list(query_db, query_object, data_scope_sql, is_page=True)
        return seedlings_list

    @classmethod
    async def get_seedlings_by_id(cls, query_db: AsyncSession, seedlings_id: int) -> SeedlingsModel:
        seedlings = await  SeedlingsDao.get_by_id(query_db, seedlings_id)
        seedlings_model = SeedlingsModel(**CamelCaseUtil.transform_result(seedlings))
        return seedlings_model


    @classmethod
    async def add_seedlings(cls, query_db: AsyncSession, query_object: SeedlingsModel) -> SeedlingsModel:
        seedlings = await SeedlingsDao.add_seedlings(query_db, query_object)
        seedlings_model = SeedlingsModel(**CamelCaseUtil.transform_result(seedlings))
        return seedlings_model


    @classmethod
    async def update_seedlings(cls, query_db: AsyncSession, query_object: SeedlingsModel) -> SeedlingsModel:
        seedlings = await SeedlingsDao.edit_seedlings(query_db, query_object)
        seedlings_model = SeedlingsModel(**CamelCaseUtil.transform_result(seedlings))
        return seedlings_model


    @classmethod
    async def del_seedlings(cls, query_db: AsyncSession, seedlings_ids: List[str]):
        await SeedlingsDao.del_seedlings(query_db, seedlings_ids)


    @classmethod
    async def export_seedlings_list(cls, query_db: AsyncSession, query_object: SeedlingsPageModel, data_scope_sql) -> bytes:
        seedlings_list = await SeedlingsDao.get_seedlings_list(query_db, query_object, data_scope_sql, is_page=False)
        mapping_dict = {
            'breedingTime': '种苗繁育时间 ',
            'description': '种苗繁育描述 ',
            'displayName': '展示用种苗名称 ',
            'managementName': '管理任务名称 ',
            'type': '种苗类型 ',
        }
        new_data = [
            {mapping_dict.get(key): value for key, value in item.items() if mapping_dict.get(key)} for item in seedlings_list
        ]
        binary_data = export_list2excel(new_data)
        return binary_data